import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class islogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取所有的cookie对象
        Cookie[] cookies = request.getCookies();
        String username = null;
        String password = null;
        //判断cookie是否为空
        if (cookies != null) {
            //不为空则遍历
            for (Cookie cookie : cookies) {
                //取出cookie的名字和值
                String cookieName = cookie.getName();
                String cookieValue = cookie.getValue();
                //判断如果用户的名字和cookie对象存的名字相同，则赋值
                if ("username".equals(cookieName)) {
                    username = cookieValue ;
                    //判断如果用户的密码名字和cookie对象存的密码名字相同，则赋值
                } else if ("password".equals(cookieName)) {
                    password = cookieValue;
                }
            }
        }
        if (username != null && password != null) {
            //JDBC连接数据库
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            boolean loginSuccess = false;
            //用于打印输出动态用户名需要
            String realname = null;
            try {
                //注册驱动
                Class.forName("com.mysql.jdbc.Driver");
                //获取连接
                conn = DriverManager.getConnection("jdbc:mysql://localhost/bjpowernode", "root", "19970913Xun");
                //关闭自动提交机制
                conn.setAutoCommit(false);
                //获取数据库操作对象
                String sql = "select username,password,realname from t_user02 where username=? and password=?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, username);
                ps.setString(2, password);
                //执行sql语句
                rs = ps.executeQuery();
                //处理查询结果集
                if (rs.next()) {
                    loginSuccess = true;
                    realname = rs.getString("realname");
                }
                //提交事务
                conn.commit();
            } catch (ClassNotFoundException e) {
                if (conn != null) {
                    try {
                        conn.rollback();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                e.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                if (ps != null) {
                    try {
                        ps.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
            }
                if (loginSuccess) {
                    //登录成功则打印成功登录信息
                    response.setContentType("text/html;charset=UTF-8");
                    PrintWriter out = response.getWriter();
                    out.print("<html>");
                    out.print("<head>");
                    out.print("<title>Success Login!</title>");
                    out.print("</head>");
                    out.print("<body>");
                    out.print("<h2 align='center'>欢迎");
                    //动态显示登录成功的用户真实姓名
                    out.print(realname);
                    out.print("登录成功！！！</h2>");
                    out.print("</body>");
                    out.print("</html>");
            } else {
                response.sendRedirect(request.getContextPath() + "/Error.html");
            }
        } else {
            response.sendRedirect(request.getContextPath() + "/login.html");
        }
    }
}
